const path = require("path");
const webpack = require("webpack");
const CompressionPlugin = require("compression-webpack-plugin");
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  .BundleAnalyzerPlugin;

const resolve = dir => path.join(__dirname, dir);
const product = process.env.NODE_ENV === "production";

// CDN资源
const assetsCDN = product
  ? {
      externals: {
        vue: "Vue",
        vuex: "Vuex",
        "vue-router": "VueRouter",
        "element-ui": "Element",
        lodash: "_",
        xlsx: "XLSX",
        axios: "axios"
      },
      css: [],
      js: [
        "//cdn.jsdelivr.net/npm/vue@2.6.11/dist/vue.min.js",
        "//cdn.jsdelivr.net/npm/vuex@3.4.0/dist/vuex.min.js",
        "//cdn.jsdelivr.net/npm/vue-router@3.2.0/dist/vue-router.min.js",
        "//cdn.jsdelivr.net/npm/element-ui@2.14.0/lib/element-ui.common.min.js",
        "//cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js",
        "//cdn.jsdelivr.net/npm/xlsx@0.16.8/dist/xlsx.min.js",
        "//cdn.jsdelivr.net/npm/axios@0.21.0/dist/axios.min.js"
      ]
    }
  : {};

// webpack 插件
const plugins = product
  ? [
      new BundleAnalyzerPlugin(),
      new CompressionPlugin({
        test: /\.js$|\.html$|.\css/,
        threshold: 10240,
        deleteOriginalAssets: false
      })
    ]
  : [];

module.exports = {
  productionSourceMap: true,
  publicPath: process.env.NODE_ENV === "production" ? "/teadmedia/" : "/",
  outputDir: "teadmedia",
  indexPath: "index.html",
  configureWebpack: {
    externals: assetsCDN,
    plugins: plugins
  },
  lintOnSave: false,

  chainWebpack: config => {
    // 别名
    config.resolve.alias.set("@", resolve("src"));
  },

  css: {}
};
// const CopyWebpackPlugin = require("copy-webpack-plugin");
// module.exports = {
//   publicPath: process.env.NODE_ENV === "production" ? "/oaceshi/" : "/oaceshi/",
//   outputDir:'oaceshi',
//   indexPath: 'index.html'
// }
